/*************************************************************************
 *  Copyright © 2023-2030 FXB CO.,LTD. All rights reserved.
 *------------------------------------------------------------------------
 *  公司：DefaultCompany
 *  项目：UPMProject
 *  文件：RefreshPackageDraw.cs
 *  作者：Administrator
 *  日期：2025/7/25 10:13:14
 *  功能：Nothing
*************************************************************************/

using SVNPackageManager.Runtime;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace SVNPackageManager.Editor
{
    [System.Serializable]
    public class RefreshPackageDraw
    {
        private bool isRefreshing = false;
        public bool IsRefreshing
        {
            get { return isRefreshing; }
            set { isRefreshing = value; }
        }

        // 每秒旋转角度，可根据需要调整
        private float rotationSpeed = 180f;
        public float RotationSpeed
        {
            set { rotationSpeed = value; }
        }

        private Texture2D rotateRefreshIcon;
        public Texture2D RotateRefreshIcon
        {
            set { rotateRefreshIcon = value; }
        }

        private float currentAngle;
        private double lastTime;

        public PackageResourceManage resourceManage;

        public RefreshPackageDraw(PackageResourceManage resourceManage)
        {
            this.resourceManage = resourceManage;
            RotateRefreshIcon = resourceManage.rotateRefreshIcon;
        }

        public void UpdateRotation()
        {
            if (!isRefreshing) return;
            double now = EditorApplication.timeSinceStartup;
            float deltaTime = (float)(now - lastTime);
            lastTime = now;
            currentAngle += rotationSpeed * deltaTime;
            currentAngle %= 360f;
        }

        public void DrawRefreshIcon(float size)
        {
            Rect iconRect = GUILayoutUtility.GetRect(size, size);
            // 保存原始矩阵
            Matrix4x4 matrixBackup = GUI.matrix;
            // 基于图标中心进行旋转
            Vector2 pivot = iconRect.center;
            GUIUtility.RotateAroundPivot(currentAngle, pivot);
            GUI.DrawTexture(iconRect, rotateRefreshIcon, ScaleMode.ScaleToFit);
            // 恢复原始矩阵
            GUI.matrix = matrixBackup;
        }
    }
}


